<html>
<head><meta charset="utf-8"><title>Type detection in tuple structs · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Type.20detection.20in.20tuple.20structs.html">Type detection in tuple structs</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="243375479"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Type%20detection%20in%20tuple%20structs/near/243375479" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Fenil Jain <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Type.20detection.20in.20tuple.20structs.html#243375479">(Jun 21 2021 at 12:37)</a>:</h4>
<p>Hey, a first time contributor here, I am trying to solve <a href="https://github.com/rust-analyzer/rust-analyzer/issues/9337">https://github.com/rust-analyzer/rust-analyzer/issues/9337</a></p>
<p>My current approach is finding the data types of variables present in tuple struct of the cursor arm and then collect all other arms with same expr and same datatypes, which will then be ORed for the final merge output.</p>
<p>I am kind of stuck in the part to determine the type of variable inside tuple struct, I am able to get the variable with the help of last_children, and then I tried approach taken in "type inference assist",  with the help of ctx.sema.type_of_, but I receive the variable as SyntaxNode, and am not sure how to go about determining its type, excuse me if its a noob doubt <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span> , any guidance would help <span aria-label="raised hands" class="emoji emoji-1f64c" role="img" title="raised hands">:raised_hands:</span> .</p>



<a name="243375634"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Type%20detection%20in%20tuple%20structs/near/243375634" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Type.20detection.20in.20tuple.20structs.html#243375634">(Jun 21 2021 at 12:39)</a>:</h4>
<p>You can use <a href="https://rust-analyzer.github.io/rust-analyzer/ide/struct.Semantics.html#method.type_of_pat"><code>Semantics::type_of_pat</code></a> to query the type of a pattern</p>



<a name="243375693"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Type%20detection%20in%20tuple%20structs/near/243375693" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Type.20detection.20in.20tuple.20structs.html#243375693">(Jun 21 2021 at 12:40)</a>:</h4>
<p>This should work for sub-patterns too, so you can just pass the inner identifier pattern and it should give you the type</p>



<a name="243376226"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Type%20detection%20in%20tuple%20structs/near/243376226" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Fenil Jain <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Type.20detection.20in.20tuple.20structs.html#243376226">(Jun 21 2021 at 12:45)</a>:</h4>
<p>If I try that, I get the type of enum which it is part of instead of the variable inside it, for example, for this code:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">enum</span> <span class="nc">Value1</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">One</span><span class="p">(</span><span class="nb">String</span><span class="p">),</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
<span class="k">match</span><span class="w"> </span><span class="bp">self</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">      </span><span class="n">Value1</span>::<span class="n">One</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">x</span><span class="p">.</span><span class="n">fmt</span><span class="p">(</span><span class="n">f</span><span class="p">),</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>I get the type as Value1 which is enum and not the x type.</p>



<a name="243376374"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Type%20detection%20in%20tuple%20structs/near/243376374" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Type.20detection.20in.20tuple.20structs.html#243376374">(Jun 21 2021 at 12:46)</a>:</h4>
<p>Are you passing <code>Value1::One(x)</code> or just the <code>x</code> as the pattern?</p>



<a name="243376634"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Type%20detection%20in%20tuple%20structs/near/243376634" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Fenil Jain <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Type.20detection.20in.20tuple.20structs.html#243376634">(Jun 21 2021 at 12:48)</a>:</h4>
<p>I am passing whole Value1::One(x) and yeah that's why I am getting  enum as type, but if I extract x using last_children I will get it as SyntaxNode which in turn cannot be passed to type_of_pat.</p>



<a name="243376831"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Type%20detection%20in%20tuple%20structs/near/243376831" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Type.20detection.20in.20tuple.20structs.html#243376831">(Jun 21 2021 at 12:50)</a>:</h4>
<p>You can cast <code>SyntaxNode</code>s to their typed counterpart via <code>ast::Node::cast(syntax_node)</code></p>



<a name="243376893"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Type%20detection%20in%20tuple%20structs/near/243376893" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Type.20detection.20in.20tuple.20structs.html#243376893">(Jun 21 2021 at 12:51)</a>:</h4>
<p>you probably don't want to extract it that way anyway though</p>



<a name="243376925"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Type%20detection%20in%20tuple%20structs/near/243376925" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Type.20detection.20in.20tuple.20structs.html#243376925">(Jun 21 2021 at 12:51)</a>:</h4>
<p>it probably makes more sense to look for bind patterns in the pattern</p>



<a name="243377631"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Type%20detection%20in%20tuple%20structs/near/243377631" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Fenil Jain <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Type.20detection.20in.20tuple.20structs.html#243377631">(Jun 21 2021 at 12:57)</a>:</h4>
<p>I am not entirely sure what you meant here <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span> .</p>



<a name="243378453"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Type%20detection%20in%20tuple%20structs/near/243378453" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Type.20detection.20in.20tuple.20structs.html#243378453">(Jun 21 2021 at 13:04)</a>:</h4>
<p>this bug isn't really about tuple structs, is it? it would probably also happen with <code>Value::Char { value: x }</code> and <code>Value::I8 { value: x }</code>. Or more complicated patterns like <code>(Value::Char(x), _)</code> and <code>(_, Value::I8(x))</code>. we need to make sure that all the match arms have the same bind patterns with the same types</p>



<a name="243378746"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Type%20detection%20in%20tuple%20structs/near/243378746" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Type.20detection.20in.20tuple.20structs.html#243378746">(Jun 21 2021 at 13:07)</a>:</h4>
<p>ideally we'd use the <code>hir</code> APIs for that, but I don't think we have a way to get all bindings introduced by some pattern there yet</p>



<a name="243390248"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Type%20detection%20in%20tuple%20structs/near/243390248" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Fenil Jain <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Type.20detection.20in.20tuple.20structs.html#243390248">(Jun 21 2021 at 14:28)</a>:</h4>
<p>Yeah it surely isn't limited to tuple structs, as in for adding the API you mentioned, if it's possible I can try to take up the task and subsequently fix the issue. Could you give some context on how and where should I start looking for the same in codebase?</p>



<a name="243395470"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Type%20detection%20in%20tuple%20structs/near/243395470" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Type.20detection.20in.20tuple.20structs.html#243395470">(Jun 21 2021 at 15:01)</a>:</h4>
<p>I think for now I'd just implement it using the AST, the HIR API is basically <a href="https://github.com/rust-analyzer/rust-analyzer/issues/8713">rust-analyzer/rust-analyzer#8713</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>